<?php
/*
 *	--------------------------------------------------------------------------------------------------------------------------
 *	Program			: HTML Form Field Generator, PHP Class Library
 *	Version			: 1.0.0
 *	Files			: htmlform.inc.php, htmlform_base.inc.php, htmlform_exc.inc.php
 *	Author			: Lasantha Samarakoon
 *	Date released	: Monday, September 21, 2009
 *	Email			: lasn1987@gmail.com
 *	Licence			: http://www.gnu.org/licenses/gpl.txt
 *	--------------------------------------------------------------------------------------------------------------------------
 *
 *	This program is a freeware, which falls under GNU Genral Public Licence.
 *	---------------------------------------------------------------------------------------------------------------------------
 *	You can modify this program, without any permission from the author. But be kind enough to send the updated version to the
 *	author through the above mentioned Email address.
 *	---------------------------------------------------------------------------------------------------------------------------
 *	Documentation:
 *			Please refer the test.php file for hints on the usage of this class library.
 *	---------------------------------------------------------------------------------------------------------------------------
 *	************************************* PROUD TO BE A SRI LANKAN...!!! ******************************************************
 */

require_once 'htmlform_exc.inc.php';		// exception classes
require_once 'htmlform_base.inc.php';		// base classes

// this class is used to generate html form element
class HTMLForm implements IGeneratable {
	
	private $properties 	= array('name' => null, 'id' => null, 'enctype' => null, 'action' => null, 'method' => null, 'className' => null);
	private $_elems			= array();
	
	// constructor
	function __construct($name = null, $id = null, $action = null, $method = null, $enctype = null, $className = null) {
		
		$this->name 		= $name;
		$this->id 			= $id;
		$this->action 		= $action;
		$this->method 		= $method;
		$this->enctype 		= $enctype;
		$this->className 	= $className;
	}
	
	function __get($key) {
		
		if(array_key_exists($key, $this->properties))
			return $this->properties[$key];
		else
			return null;
	}
	
	function __set($key, $val) {
		
		if(array_key_exists($key, $this->properties))
			$this->properties[$key] = $val;
	}
	
	function generate() {
		
		// create form
		$r = "<form";
		
		if($this->name 		!= null) $r .= " name=\"{$this->name}\"";
		if($this->id 		!= null) $r .= " id=\"{$this->id}\"";
		if($this->enctype 	!= null) $r .= " enctype=\"{$this->enctype}\"";
		if($this->action 	!= null) $r .= " action=\"{$this->action}\"";
		if($this->method 	!= null) $r .= " method=\"{$this->method}\"";
		if($this->className != null) $r .= " class=\"{$this->className}\"";
		
		$r .= ">\n";
		
		// add components
		foreach($this->_elems as $e) {
			$r .= $e->generate();
		}
		
		// close form
		$r .= "</form>\n";
		
		// return html code
		return $r;
	}

	// this function is used to add form elements to the form
	function addElement($elem, $elname) {

	// check all required parameters are not null
		if($elem != null && $elname != null)
			$this->_elems[$elname] = $elem;
		else
			throw new NullElementException();
	}

	// this function is used to remove elements from the form
	function removeElement($elname) {
	
		// temporary array to save filtered components
		$new_elems = array();
		
		// traverse through the component collection
		foreach($this->_elems as $k=>$v)
			if(strtolower($k) != strtolower($elname))
				$new_elems[$k] = $v;	// save filtered components to the new temporary array
				
		// set the reproduced array as the component collection
		$this->_elems = $new_elems;
	}
}

// create html text input command
class TextField extends TextFieldBase implements IGeneratable {
	
	function generate() {
		return parent::generate_field('text');
	}
}

// create html hidden input command
class HiddenField extends TextFieldBase implements IGeneratable {
	
	function generate() {
		return parent::generate_field('hidden');
	}
}

// create html password input command
class PasswordField extends TextFieldBase implements IGeneratable {
	
	function generate() {
		return parent::generate_field('password');
	}
}

// create html checkbox command
class CheckBox extends CheckboxBase implements IGeneratable {
	
	function generate() {
		return parent::generate_field('checkbox');
	}
}

// create html radio button command
class Radio extends CheckboxBase implements IGeneratable {
	
	function generate() {
		return parent::generate_field('radio');
	}
}

// create html textarea element
class Textarea extends FormField implements IGeneratable {
	
	// constructor
	function __construct($name = null, $id = null, $value = null, $className = null, $disabled = false) {

		// invoke super class constructor
		parent::__construct($name, $id, $className, $disabled);
		
		$this->properties['value'] = $value;
	}

	function generate() {
		
		$r = "<textarea";
		// check for available attributes
		if($this->name 		!= null) $r .= " name=\"{$this->name}\"";
		if($this->id 		!= null) $r .= " id=\"{$this->id}\"";
		if($this->className != null) $r .= " class=\"{$this->className}\"";
		if($this->disabled) 		 $r .= " disabled=\"disabled\"";
		
		$r .= ">{$this->value}</textarea>\n";
		//return html coding
		return $r;
	}
}

// create html submit button command
class SubmitButton extends ButtonBase implements IGeneratable {
	
	function generate() {
		return parent::generate_field('submit');
	}
}
// create html reset button command
class ResetButton extends ButtonBase implements IGeneratable {
	
	function generate() {
		return parent::generate_field('reset');
	}
}
// create html button command
class GeneralButton extends ButtonBase implements IGeneratable {
	
	function generate() {
		return parent::generate_field('button');
	}
}
// create html label element
class Label implements IGeneratable {
	
	private $properties = array('value' => null, 'id' => null, 'rel' => null, 'className' => null);

	// constructor 
	function __construct($value = null, $rel = null, $id = null, $className = null) {
		
		$this->id 			= $id;
		$this->value 		= $value;
		$this->rel 			= $rel;
		$this->className 	= $className;
	}
	
	function __get($key) {
		
		if(array_key_exists($key, $this->properties))
			return $this->properties[$key];
		else
			return null;
	}
	
	function __set($key, $val) {
		
		if(array_key_exists($key, $this->properties))
			$this->properties[$key] = $val;
	}
	
	function generate() {
		
		$r = "<label";
		// check for available attributes
		if($this->id 		!= null) $r .= " id=\"{$this->id}\"";
		if($this->rel 		!= null) $r .= " rel=\"{$this->rel}\"";
		if($this->className != null) $r .= " class=\"{$this->className}\"";
		
		$r .= ">{$this->value}</label>\n";
		// return html coding
		return $r;
	}
}

// create html select box command
class SelectBox extends FormField implements IGeneratable {
	
	private $_options = array();

	// constructor
	function __construct($name = null, $id = null, $className = null, $disabled = false) {
		// invoke super class constructor
		parent::__construct($name, $id, $className, $disabled);
	}
	
	// this function is used to add options to the select box
	function addOption($key, $text, $value = null, $selected = false) {
		
		$sbo = new SBOption($text, $value, $selected);
		$this->_options[$key] = $sbo;
	}

	// this function is used to remove options from the select box
	function removeOption($optname) {
	
		// temporary array to save filtered options
		$new_options = array();
		
		// traverse through the option collection
		foreach($this->_options as $k=>$v)
			if(strtolower($k) != strtolower($optname))
				$new_options[$k] = $v;
		
	// set the reproduced array as the option collection
		$this->_options = $new_options;
	}
	
	function generate() {
		
		$r = "<select";
		// check for available attributes
		if($this->name 		!= null) $r .= " name=\"{$this->name}\"";
		if($this->id 		!= null) $r .= " id=\"{$this->id}\"";
		if($this->className != null) $r .= " class=\"{$this->className}\"";
		if($this->disabled)			 $r .= " disabled=\"disabled\"";
		$r .= ">\n";
		
		// add options
		foreach($this->_options as $o)
			$r .= $o->generate();
		
		$r .= "</select>\n";
		// return html coding
		return $r;
	}
}

// this class is used to define select box option
class SBOption implements IGeneratable {
	
	private $properties = array('value' => null, 'selected' => false, 'text' => null);

	// constructor
	function __construct($text = null, $value = null, $selected = false) {
		
		$this->value 	= $value;
		$this->text 	= $text;
		$this->selected = $selected;
	}
	
	function __get($key) {
		
		if(array_key_exists($key, $this->properties))
			return $this->properties[$key];
		else
			return null;
	}
	
	function __set($key, $val) {
		
		if($key == 'selected')
			if(!is_bool($val))
				throw new InvalidParameterException("boolean (true or false)");
		
		if(array_key_exists($key, $this->properties))
			$this->properties[$key] = $val;
	}
	
	function generate() {
		
		$r = "<option";
		// check for available attributes
		if($this->value 	!= null) $r .= " value=\"{$this->value}\"";
		if($this->selected) 		 $r .= " selected=\"selected\"";
		
		$r .= ">{$this->text}</option>\n";
		// return html coding
		return $r;
	}
}
?>